/* GSManufactureWriteAction.java
 *
 * Project/Product: APEP-JZCG
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   peng.ning	2008-10-28	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2008
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package com.hitrust.gs.action;

import java.util.ArrayList;

import system.LoginContext;

import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.bean.GSProductTemporaryMaintainBean;
import com.hitrust.gs.bean.GSProductUpdateIsbuyBean;
import com.hitrust.gs.bean.GSProductUpdatePriceBean;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.dao.GSManufactureDAO;
import com.hitrust.gs.form.GSProductUpdateIsbuyForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSProductUpdateIsbuyView;


public class GSProductUpdateIsbuyWriteAction extends GSBaseAction {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.apep.jzcg.service.GSBaseAction#execute(com.apep.jzcg.service.GSBaseForm
	 * )
	 */
	public String execute(GSBaseForm aForm) throws Exception {

		GSManufactureProcessor processor = new GSManufactureProcessor();
		GSProductUpdateIsbuyForm form = (GSProductUpdateIsbuyForm) aForm;
		LoginContext context = (LoginContext) form.getSession().getAttribute(
				GSBaseConstants.LOGIN);
		String type=(String)aForm.getRequest().getParameter("type");
		
		String[] index = form.getSel_manufacture();
		String[] rid = form.getGs_recorder_id();
		
		ResultProcess rst =null;
		if("0".equals(type)){
			 rst = (ResultProcess) aForm.getSession().getAttribute(
				"productUpdateIsbuyOffExcel");
		}else{
			 rst = (ResultProcess) aForm.getSession().getAttribute(
				"productUpdateIsbuyOnExcel");
		}

		ArrayList array = rst.getAllRecoreds();
		boolean bl = false;
		try {	
			for (int a = 0; a < index.length; a++) {
				int i = 0;
				for (int tJ = 0; tJ < rid.length; tJ++) {
					if (index[a].equals(rid[tJ])) {
						i = tJ;
						break;
					}
				}
				String concentricityCode = form.getGs_concentricity_code()[i].trim();
				String enabledtime= form.getGs_enabled_time()[i].trim();
				String recordid= form.getGs_recorder_id()[i].trim();
				int xh=Integer.parseInt(recordid)+1;
				GSManufactureDAO dao = new GSManufactureDAO();
				if("0".equals(type)){
		
				GSProductUpdateIsbuyBean pubean = dao.getProductUpdateIsbuyByConcentricityCodeAndDate(
						concentricityCode, enabledtime,"0"," select * from t_product_update_isbuy ");
				
				if(pubean!=null){
					if("0".equals(pubean.getGs_auditing_status())||"1".equals(pubean.getGs_auditing_status())){
						throw new GSException(
								"序号"+ xh + ": "
								+ "在启用日期里里已经存在相同记录!");
					}
				}
				
				GSProductTemporaryMaintainBean tempBean=dao.getProductTemporaryMaintainByConcentricityCodeAndLessDate(
						concentricityCode, enabledtime," select * from t_product_temporary_maintain ");
				
				if(tempBean!=null){
					if(tempBean.getGs_auditing_status()!=null&&!tempBean.getGs_auditing_status().equals("2")){//判断是否是审批不通过的记录,不通过不用判断
						if("0".equals(tempBean.getGs_status())||"2".equals(tempBean.getGs_status())){
							throw new GSException(
									"序号"+ xh + ": "
									+ "在启用日期或之前有替换!");
						}
					}

				}
				
				GSProductUpdatePriceBean priceBean=dao.getProductUpdatePriceByConcentricityCodeAndLessDate(
						concentricityCode, enabledtime," select * from t_product_update_price ");
				
				if(priceBean!=null){
					if(priceBean.getGs_auditing_status()!=null&&!priceBean.getGs_auditing_status().equals("2")){//判断是否是审批不通过的记录,不通过不用判断
						if("0".equals(priceBean.getGs_status())||"2".equals(priceBean.getGs_status())){
							throw new GSException(
									"序号"+ xh + ": "
									+ "在启用日期或之前有调价!");
						}
					}
				}
				
				}else{				
					GSProductUpdateIsbuyBean pubean = dao.getProductUpdateIsbuyByConcentricityCodeAndDate(
							concentricityCode, enabledtime,"1"," select * from t_product_update_isbuy ");
					
					if(pubean!=null){
						if("0".equals(pubean.getGs_auditing_status())||"1".equals(pubean.getGs_auditing_status())){
							throw new GSException(
									"序号"+ xh + ": "
									+ "在启用日期里里已经存在相同记录!");
						}
					}
					
				}
				
				

			}
			
			
			
				for (int a = 0; a < index.length; a++) {
					int i = 0;
					for (int tJ = 0; tJ < rid.length; tJ++) {
						if (index[a].equals(rid[tJ])) {
							i = tJ;
							break;
						}
					}

				
					
					String concentricityCode = form.getGs_concentricity_code()[i].trim();
					String enabledtime= form.getGs_enabled_time()[i].trim();
					String producttype="";
					String productbid="";
					if("1".equals(type)){
						producttype=form.getGs_product_type()[i].trim();
						productbid=form.getGs_product_bid()[i].trim();
					}
					
					
					GSManufactureDAO dao = new GSManufactureDAO();
					GSManufactureBean mbean = dao.getManufactureByConcentricityCode(
							concentricityCode, " select * from t_product ");
					
	
					
					GSProductUpdateIsbuyBean pbean=new GSProductUpdateIsbuyBean();
					pbean.setGs_concentricity_code(concentricityCode);
					pbean.setGs_product_id(mbean.getId());
					pbean.setGs_create_user_id(context.getLoginUserId());
					pbean.setGs_create_user(context.getLoginUserName());
					pbean.setGs_create_time(new framework.core.DateUtil().getFullString());
					pbean.setGs_enabled_time(enabledtime);
					pbean.setGs_auditing_status("0");
					pbean.setGs_status("0");
					if("0".equals(type)){
						pbean.setGs_type("0");
					}else{
						pbean.setGs_type("1");
						pbean.setGs_product_type(producttype);
						if(productbid!=null&&!"".equals(productbid)){
							pbean.setGs_product_bid(productbid);
						}
					}
					bl=dao.insertProductUpdateIsbuy(pbean);
					
					
					
					if (bl) {
						// 移除
						int row = array.size();
						for (int j = 0; j < row; j++) {
							GSProductUpdateIsbuyView mv = (GSProductUpdateIsbuyView) array
									.get(j);
							if (index[a].equals(mv.getGs_recorder_id())) {
								array.remove(mv);
								break;
							}
						}
					}
				}
				
				if (array != null && array.size() == 0) {
					if("0".equals(type)){
						aForm.getSession().removeAttribute("productUpdateIsbuyOffExcel");
						aForm.getRequest().setAttribute("message",
								"产品关闭信息确认写入成功，无可确认产品关闭信息，返回继续添加。");
					}else{
						aForm.getSession().removeAttribute("productUpdateIsbuyOnExcel");
						aForm.getRequest().setAttribute("message",
								"产品启用信息写入成功，无可确认产品启用信息，返回继续添加。");
					}
					aForm.getRequest().setAttribute("action", "A190008.do?type="+type);
	
				} else if (array != null && array.size() > 0) {
					if("0".equals(type)){
						aForm.getRequest().setAttribute("message", "产品关闭信息确认写入成功");
					}else{
						aForm.getRequest().setAttribute("message","产品启用信息写入成功");
					}
					
					if (aForm.getRequest().getParameter("choice").equals("")) {
					    aForm.getRequest().setAttribute("action", "A190010.do?type="+type);
					}
				}
		} catch (GSException e) {
			GSLogWriter.error(this, e.toString());
		    aForm.getRequest().setAttribute("action", "A190010.do?type="+type);
			aForm.getRequest().setAttribute("target", "_self");
			throw new GSException(e.getMessage());
		} finally {
			rst.setResult(array);
			if("0".equals(type)){
				aForm.getSession().setAttribute("productUpdateIsbuyOffExcel", rst);
			}else{
				aForm.getSession().setAttribute("productUpdateIsbuyOnExcel", rst);
			}
		}
		return GSBaseConstants.FORWARD_SUCCESS;
	}
}
